package com.yang.guliproduct.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.guliproduct.entity.PmsAttr;
import com.yang.guliproduct.service.impl.PmsAttrServiceImpl;
import com.yang.guliproduct.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/guliproduct/pms-attr")
public class PmsAttrController {

    @Autowired
    private PmsAttrServiceImpl attrService;

    @PostMapping("addAttr")
    public R addAttr(@RequestBody PmsAttr pmsAttr){
        attrService.save(pmsAttr);
        return R.ok();
    }

    @PostMapping("updateAttr")
    public R updateAttr(@RequestBody PmsAttr pmsAttr){
        attrService.updateById(pmsAttr);
        return R.ok();
    }

    @DeleteMapping("deleteAttr/{id}")
    public R deleteAttr(@PathVariable Long id){
        attrService.removeById(id);
        return R.ok();
    }

    @GetMapping("getByIdAttr/{id}")
    public R getByIdAttr(@PathVariable Long id){
        PmsAttr attr = attrService.getById(id);
        return R.ok().data("attr",attr);
    }

    @PostMapping("getAttrPage/{current}/{limit}")
    public R getAttrPage(@PathVariable Long current, @PathVariable Long limit,
                         @RequestBody(required = false) PmsAttr pmsAttr){
        QueryWrapper<PmsAttr> queryWrapper = new QueryWrapper<>();
        Page<PmsAttr> page = new Page<>(current,limit);
        Long catelogId = pmsAttr.getCatelogId();
        if (!StringUtils.isEmpty(catelogId)){
            queryWrapper.eq("catelog_id",catelogId);
        }
        attrService.page(page, queryWrapper);
        long total = page.getTotal();
        List<PmsAttr> records = page.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

}

